package view;

import java.awt.BorderLayout;
import java.util.ResourceBundle;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;
import javax.swing.text.BadLocationException;

import controller.PlayButtonListener;

/**
 * @author Andrea
 * 
 */
public class InputArea {
	private static ResourceBundle myViewLabels = ResourceBundle
			.getBundle("resources.englishView");
	private static ResourceBundle myViewSizes = ResourceBundle
			.getBundle("resources.viewSizes");

	private JPanel inputPanel;
	private JTextArea inputBox;
	private String newline = "\n";
	private String lastInput;
	private int inputLines;

	public JPanel createInputTextArea() {
		// Initialize the panel
		inputPanel = new JPanel(new BorderLayout());

		// Make text box to go in it
		GeneralTextArea in = new GeneralTextArea();
		inputBox = in.makeTextField(myViewLabels.getString("InputPanel"),
				Integer.parseInt(myViewSizes.getString("InputFieldRows")),
				ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, true);

		// Put in the prompt
		inputBox.setText(myViewLabels.getString("InputPrompt"));

		// Make the run button
		JButton playButton = new JButton(myViewLabels.getString("PlayButton"));

		playButton.addActionListener(new PlayButtonListener(this));

		// Add components to panel
		inputPanel.add(inputBox);
		inputPanel.add(playButton, BorderLayout.PAGE_END);
		return inputPanel;
	}

	// Returns the last thing that was typed into the input box
	public String getLastInput() {
		return lastInput;
	}

	public JTextArea getInputBox() {
		return inputBox;
	}

	public String simplifyText() {
		lastInput = "";
		inputLines = inputBox.getLineCount();

		// Load input into a string and format it...
		// If it's one line long, you can just grab the text
		if (inputLines == 1) {
			lastInput = inputBox.getText();
		}

		// Otherwise, JTextArea requires that you do this weird chunking thing
		// in order to get each line separately and ensure there are spaces
		// between commands on different lines so that the parser can handle
		// them properly.
		else {
			for (int i = 0; i < inputLines; i++) {
				int lineStartIndex = 0;
				int lineEndIndex = 0;
				try {
					lineStartIndex = inputBox.getLineStartOffset(i);
					lineEndIndex = inputBox.getLineEndOffset(i);
				} catch (BadLocationException e2) {
					e2.printStackTrace();
				}

				try {
					lastInput += (inputBox.getText(lineStartIndex,
							((lineEndIndex - lineStartIndex))));
				} catch (BadLocationException e1) {
					e1.printStackTrace();
				}
				lastInput += newline;
			}
		}

		String promptSymbol = myViewLabels.getString("InputPrompt").substring(
				0, 1);
		// If the prompt symbol is in the string we're parsing, get rid
		// of it
		if (lastInput.contains(promptSymbol))
			lastInput = lastInput.replaceAll(promptSymbol, "");
		lastInput.trim();

		// Add input to the history
		HistoryArea.addToHistory(lastInput);

		// Reset the input box
		inputBox.setText(myViewLabels.getString("InputPrompt"));

		return lastInput;
	}
}
